<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * MY_Email - Allows for email config settings to be stored in the db.
 * 
 * @author      Stephen Cozart - PyroCMS dev team
 * @package 	PyroCMS
 * @subpackage  library
 * @category	email   
 */
class MY_Email extends CI_Email {
    
    protected $ci;
    
    /**
     * Constructor method
     *
     * @access public
     * @return void
     */
    function __construct()
    {
        parent::__construct();
        
        $this->ci =& get_instance();
        
        //set mail protocol
        $config['protocol'] = $this->ci->settings->mail_protocol;
        
        //set a few config items (duh)
        $config['mailtype']	= 'html';
        $config['charset']	= 'utf-8';
        $config['crlf']		= '\r\n';
        $config['newline']	= '\r\n';
        
        //sendmail options
        if ($this->ci->settings->mail_protocol == 'sendmail')
        {
                if($this->ci->settings->mail_sendmail_path == '')
                {
                        //set a default
                        $config['mailpath'] = '/usr/sbin/sendmail';
                }
                else
                {
                        $config['mailpath'] = $this->ci->settings->mail_sendmail_path;
                }
        }
        
        //smtp options
        if ($this->ci->settings->mail_protocol == 'smtp')
        {
                $config['smtp_host'] = $this->ci->settings->mail_smtp_host;
                $config['smtp_user'] = $this->ci->settings->mail_smtp_user;
                $config['smtp_pass'] = $this->ci->settings->mail_smtp_pass;
                $config['smtp_port'] = $this->ci->settings->mail_smtp_port;
        }
        
        $this->initialize($config);
    }
	function send_email()
	{
		$this->load->library('email');
		$this->load->library('user_agent');
		$this->email->from($this->input->post('contact_email'), $this->input->post('contact_name'));
		$this->email->to($this->settings->item('contact_email'));

		// If "other subject" exists then use it, if not then use the selected subject
		$subject = 'customer contact us';
		$this->email->subject($this->settings->item('site_name') .' - '.$subject);

		// Loop through cleaning data and inputting to $data
		foreach(array_keys($_POST) as $field_name)
		{
			$data[$field_name] = $this->input->post($field_name, TRUE);
		}

		// Add in some extra details
		$data['sender_agent']	=	$this->agent->browser().' '.$this->agent->version();
		$data['sender_ip']		=	$this->input->ip_address();
		$data['sender_os']		=	$this->agent->platform();

		$this->email->message($this->load->view('email/contact_html', $data, TRUE));
		$this->email->set_alt_message($this->load->view('email/contact_plain', $data, TRUE));

		// If the email has sent with no known erros, show the message
		return (bool) $this->email->send();
	}    
}
/* End of file MY_Email.php */